<template>
  <div>
    <BasicTable @register="registerTable" ref="tableRef">
      <template #fromInfo="{ record }">
        <Button color="success" @click="clickFromInfo(record)">按钮</Button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              label: '分配规则',
              icon: 'clarity:note-edit-line',
              onClick: handleAssignJob.bind(null, record),
            },
          ]"
        />
      </template>
    </BasicTable>
    <Modal v-model:visible="visible" title="表单详情" @ok="handleOk" width="800px">
      <form-create :option="option" :rule="rule" />
    </Modal>
  </div>
</template>
<script lang="ts">
import { defineComponent, reactive, toRefs, unref, ref } from 'vue';
import { Modal } from 'ant-design-vue';
import { Button } from '/@/components/Button';
import { BasicTable, useTable, TableAction, TableActionType } from '/@/components/Table';
import { modeDefineList } from '/@/api/backs/workFlow/form';
import { useDrawer } from '/@/components/Drawer';
import { decodeFields } from '/@/utils/formCreate';
import { useRoute } from 'vue-router';
import { useGo } from '/@/hooks/web/usePage';

import { columns, searchFormSchema } from './components/tableData';

export default defineComponent({
  name: 'ModelDefine',
  components: { BasicTable, TableAction, Button, Modal },
  props: {},
  setup() {
    const go = useGo();
    const data: any = reactive({
      type: 'add',
      visible: false,
      rule: [],
      option: {},
    });
    // const detailData = ref({
    //   rule: [],
    //   option: {},
    // });
    const { params } = useRoute(); // 路由信息
    const tableRef = ref<Nullable<TableActionType>>(null);
    const getTableAction = () => {
      const tableAction = unref(tableRef);
      if (!tableAction) {
        throw new Error('tableAction is null');
      }
      return tableAction;
    };

    const [registerDrawer, { openDrawer, closeDrawer }] = useDrawer();
    const [registerTable, { reload, setSelectedRowKeys }] = useTable({
      title: '流程定义',
      api: modeDefineList,
      columns,
      formConfig: {
        labelWidth: 100,
        schemas: searchFormSchema,
      },
      searchInfo: {
        key: params.id,
      },
      useSearchForm: false,
      showTableSetting: true,
      actionColumn: {
        title: '操作',
        dataIndex: 'action',
        slots: { customRender: 'action' },
        fixed: 'right',
      },
    });
    const getSelectRowList = () => {
      return getTableAction().getSelectRows();
    };
    const setSelectedRowKeyList = (arr) => {
      setSelectedRowKeys(arr);
    };
    console.log('---------', data.visible);
    //查看表单信息
    const clickFromInfo = (record: Recordable) => {
      console.log('我是表单信心', record);
      if (record.formType == 10) {
        // 设置表单
        data.option = JSON.parse(record.formConf);
        data.rule = decodeFields(record.formFields);
        // 弹窗打开
        data.visible = true;
      }
      console.log('---------', data.visible);
    };
    const handleOk = () => {
      data.visible = false;
      console.log('---------', data.visible);
    };
    //分配规则
    function handleAssignJob(record: Recordable) {
      go('/backs/workFlow/modelAllocation/' + record.id);
    }
    return {
      ...toRefs(data),
      tableRef,
      registerTable,
      registerDrawer,
      getSelectRowList,
      setSelectedRowKeyList,
      handleAssignJob,
      clickFromInfo,
      handleOk,
    };
  },
});
</script>
